package cn.ante.system.controller;

import cn.ante.basic.annotation.PreAuthorize;
import cn.ante.system.service.IRoleService;
import cn.ante.system.domain.Role;
import cn.ante.system.query.RoleQuery;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "角色的api",description="角色相关的crud功能")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "保存/修改角色")
    @PreAuthorize(name ="保存/修改角色",sn="role:addOrUpt")
    public AjaxResult addOrUpdate(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.add(role);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "根据id删除角色")
    @PreAuthorize(name ="根据id删除角色",sn="role:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            roleService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询角色")
    @PreAuthorize(name ="根据id查询角色",sn="role:get")
    public Role get(@PathVariable("id")Long id)
    {
        return roleService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Role> list(){

        return roleService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询角色")
    @PreAuthorize(name ="分页查询角色",sn="role:list")
    public PageList<Role> json(@RequestBody RoleQuery query)
    {
        return roleService.findAll(query);
    }
}
